layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 管理
     */
    var ShiProductFunction = {
        tableId: "shiProductFunctionTable"
    };

    /**
     * 初始化表格的列
     */
    ShiProductFunction.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '产品功能表，id'},
            {field: 'productOrderName', sort: true, title: '产品名称'},
            // {field: 'heads', sort: true, title: '头部功能'},
            // {field: 'shoulder', sort: true, title: '肩部功能'},
            // {field: 'backs', sort: true, title: '背部功能'},
            // {field: 'buttocks', sort: true, title: '臀部功能'},
            // {field: 'handrail', sort: true, title: '扶手功能'},
            // {field: 'calfs', sort: true, title: '小腿功能'},
            // {field: 'appends', sort: true, title: '附加功能'},
            {field: 'basicParameter', sort: true, title: '基本参数'},
            {field: 'productSpecification', sort: true, title: '产品规格'},
            {field: 'remarks', sort: true, title: '产品备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    ShiProductFunction.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(ShiProductFunction.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    ShiProductFunction.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/shiProductFunction/add';
    };

    /**
     * 导出excel按钮
     */
    ShiProductFunction.exportExcel = function () {
        var checkRows = table.checkStatus(ShiProductFunction.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ShiProductFunction.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/shiProductFunction/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ShiProductFunction.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/shiProductFunction/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ShiProductFunction.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ShiProductFunction.tableId,
        url: Feng.ctxPath + '/shiProductFunction/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: ShiProductFunction.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ShiProductFunction.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ShiProductFunction.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ShiProductFunction.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ShiProductFunction.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ShiProductFunction.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ShiProductFunction.onDeleteItem(data);
        }
    });


    //产品对比
    $('#contrast').click(function () {
        var a;
        var checkRows = table.checkStatus(ShiProductFunction.tableId);
        for (var i = 0; i<=checkRows.data.length; i++){
            a += "," + checkRows.data[i].id;
            window.location.href = Feng.ctxPath + '/shiProductFunction/edit?id='+a;
        }

    });




});
